package other;

/**
 * Created by lyyn on 2022/7/21.
 */
public class YiChuYuanSu_24 {
    public static void main(String[] args) {
        int[] nums = new int[]{0,1,2,2,3,0,4,2};
        int n = new Solution_1().removeElement_2(nums, 2);
        System.out.println(n);
        for (int i = 0; i < n; i++) {
            System.out.println(nums[i]);
        }
    }
}
class Solution_1 {
    //暴力破解
    public int removeElement_1(int[] nums, int val) {
        int n = nums.length;
        for (int i = 0; i < n; i++) {
            if(nums[i] == val){
                int d = i;
                for (int j = d + 1; j < n; j++) {
                    nums[d] = nums[j];
                    d++;
                }
                n--;
                i--;
            }
        }
        return n;
    }
    //快慢指针 0,1,2,2,3,0,4,2
    public int removeElement_2(int[] nums, int val){
        int fast = 0;
        int low = 0;
        for ( ; fast < nums.length; fast++) {
            if(nums[fast] != val){
                nums[low] = nums[fast];
                low++;
            }
        }
        return low;
    }
}